package com.crm.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import com.crm.dao.NewsDao;
import com.crm.entity.News;
import com.crm.service.NewsService;

@Service
public class NewsServiceImpl implements NewsService {

	@Autowired
	private NewsDao newsDao;

	@Override
	public List<News> findNews() {
		return newsDao.findAll();
	}

	@Override
	public void addNews(News news) {
		news.setAddTime(new Date());
		if(news.getClick() == null) {
			news.setClick(0);
		}
		newsDao.save(news);
	}

	@Override
	public News findNewsById(String id) {
		if(StringUtils.isNotBlank(id)) {
			return newsDao.findOne(id);
		}
		return null;
	}

	@Override
	public Page<News> findNewsByPage(Integer pageIndex,String title) {
		News news = new News();
		if(StringUtils.isNotBlank(title)) {
			news.setTitle(title);
		}
		//创建实例
        Example<News> example = Example.of(news);
        PageRequest pageable = new PageRequest(pageIndex,10,Sort.Direction.DESC,"addTime");
		
		return newsDao.findAll(example, pageable);
	}
	
}
